<template>
	<view class="balance">
		<!-- #ifndef H5 -->
		<topTitle :styles="{back:true,title:'我的余额',color:'#fff'}" />
		<!-- #endif -->
		<view class="balance_num">
			<text>当前余额 (元)</text>
			<text class="num">{{yue}}</text>
			<!-- <text class="refund">申请退款</text> -->
			<text class="refund" style="background: transparent;"></text>
		</view>
		<view class="balance_record">
			<view class="recharge" @click="record">充值记录</view>
			<view class="consumption" @click="detail">消费明细</view>
			<view class="refund" @click="refund">退款记录</view>
		</view>
		<view class="recharge_money">
			<view class="money">
				<view :class="{'active': current == index}" class="money_item" v-for="(item,index) in rechargeList" :key="index" @click="chexPrice(index)">
					<!-- <view class="button"> -->
						<text class="principal">{{item.money}}</text>
						<text class="give">送{{item.donate_money}}</text>
					<!-- </view> -->
				</view>
			</view>
			<view class="technician">
				<view class="title">
					填写技师id：
				</view>
				<input type="text" v-model="technician">
			</view>
			<view class="custom">
				<input type="text" v-model="customPrice" @input="fpNumInput" placeholder="自定义充值金额" placeholder-style="font-size: 12px;color: rgb(138, 138, 138);">
			</view>
		</view>
		<view class="submit" @click="submit">
			充值并支付
		</view>
		<view class="explain">
			<p>1.充值的本金和赠送金额均不可提现、转移、转赠。</p>
			<p>2.充值套餐赠送金额部分不可进行退款。</p>
			<p>3.使用范围：本平台所有项目购买，以及交通费用支付。</p>
		</view>
		<view class="payy" v-if="pay_flag">
			<!-- 遮罩层 -->
			<view class="top_seat" @click.stop="nopay"></view>
			<view class="content">
				<view class="head">
					<view class="cancel" @click.stop="nopay">
						取消
					</view> 
				</view>
				<view class="payList">
					<view class="list" @click.stop="paySubmit('wxpay')">
						<image src="https://oos.kmey.cn/50/226d0a7918294ab838621ac05186cc.png" mode=""></image>
						<text>微信支付</text>
					</view>
					<view class="list" @click.stop="paySubmit('alipay')">
						<image src="/static/images/orders/alipay.png" mode=""></image>
						<text>支付宝</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef H5
	import wx from "weixin-js-sdk"
	// #endif
	import topTitle from "@/components/topTitle.vue"
	export default {
		components:{
			topTitle
		},
		data() {
			return {
				yue:0,
				current:0,
				rechargeList:[],
				customPrice:'',
				recharge_money:{},
				technician:'',
				pay_flag:false,
			};
		},
		onLoad(option) {
			this.yue = option.yue
			this.getBlanceList()
		},
		watch:{
			customPrice(newValue,oldValue){
				this.current = null;
				this.recharge_money = {}
			}
		},
		methods:{
			nopay(){
				this.pay_flag = false;
			},
			//选择充值方案
			chexPrice(i){
				this.current = i
				this.recharge_money = JSON.parse(JSON.stringify(this.rechargeList[i]))
			},
			changePrice(e){
				console.log(e)
			},
			fpNumInput(e){
				const o = e.target;
				const inputRule =/^(0+)|[^\d]+/g//
				this.$nextTick(function(){
					if(inputRule.test(o.value)){
						uni.showToast({
							title:'请输入数字金额',
							icon:"error"
						})
						this.customPrice = ''
					}
				})
			},
			//获取充值列表
			getBlanceList(){
				this.$http({
					url:'/api/recharge/list',
					method:'get',
					success:result=>{
						console.log(result,'123123123')
						if(result.code == 200){
							this.rechargeList = result.data
							this.recharge_money = result.data[0]
						}
					}
				})
			},
			submit(){
				if(!this.recharge_money.money && this.customPrice == ""){
					uni.showToast({
						title:'请选择充值方案或输入充值金额',
						icon:"none"
					})
					return false;
				}
				this.pay_flag = true;
			},
			//充值
			paySubmit(type){
				uni.showLoading({
					title:'支付中...'
				})
				if(this.customPrice != ""){
					var data ={
						money:this.customPrice,
						technician_id:this.technician,
					}
				}else{
					var data ={
						money:this.recharge_money.money,
						technician_id:this.technician,
					}
				}
				//创建充值订单
				this.$http({
					url:'/api/v2/recharge/order/create',
					method:'post',
					data:data,
					success:result=>{
						console.log(result,'----------------充值')
						if(result.code == 200){
							this.pay_flag = false;
							this.pay(result.data.info,type)
						}
						uni.hideLoading()
					}
				})
			},
			//支付订单
			pay(info,type){
				info.pay_type = type;
				info.from = this.$from()
				this.$http({
					url:'/api/v2/pay_order',
					method:'post',
					data:info,
					success:result=>{
						console.log(result,'----------------充值')
						if(result.code == 200){
							if(result.data.info.js_config){
								var data = result.data.info.js_config;
								// #ifdef H5
								wx.config({
									debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
									appId:data.appId, // 必填，公众号的唯一标识
									timestamp:data.timestamp , // 支付签名时间戳，注意微信 jssdk 中的所有使用 timestamp 字段均为小写。但最新版的支付后台生成签名使用的 timeStamp 字段名需大写其中的 S 字符
									nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
									signType:data.signType, // 微信支付V3的传入 RSA ,微信支付V2的传入格式与V2统一下单的签名格式保持一致
									jsApiList: ['invoke'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
								});
								wx.invoke(
									'getBrandWCPayRequest', {
										"appId": data.appId, //公众号ID，由商户传入     
										"timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数     
										"nonceStr": data.nonceStr, //随机串     
										"package": data.package,
										"signType": data.signType, //微信签名方式：     
										"paySign": data.paySign, //微信签名 
									},(res)=>{
										console.log(res,'----------------res')
									}
								)
								// #endif
								
							}else if(result.data.info.scheme_url){
								let url = result.data.info.scheme_url;
								// #ifdef H5
								if (uni.getSystemInfoSync().platform =='android') { //android终端
									window.open(url);
								} else if (uni.getSystemInfoSync().platform == "ios") { //ios终端
									window.location.href = url;
								}
								// #endif
								// #ifdef APP-PLUS
								plus.runtime.openURL(encodeURI(url), err => {
									console.log(err)
								})
								// #endif
							}
						}
						uni.hideLoading()
					}
				})
				
			},
			record(){
				uni.navigateTo({
					url:'/pages/my/balance/record/record'
				})
			},
			detail(){
				uni.navigateTo({
					url:'/pages/my/balance/detail/detail'
				})
			},
			refund(){
				uni.navigateTo({
					url:'/pages/my/balance/refund/refund'
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
	}
	.balance {
		width: 100%;
		height: 100%;
		background-color: #F6F6F6;
		.balance_num {
			height: 135px;
			border-radius: 15px;
			margin: 22px;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			background: url('https://oos.kmey.cn/0b/bb250d1704905fd3a3d87e8b4850aa.png');
			background-size: 100% 100%;
			box-shadow: #67d59e 0px 5px 10px;
			>text{
				margin-left: 22px;
				color: #fff;
				font-size: 14px;
			}
			>.num{
				font-size: 30px;
			}
			.refund{
				width: 79px;
				height: 23px;
				text-align: center;
				color: #12C86E;
				background-color: #D3FBE6;
				font-size: 13px;
				padding: 2px 3px;
				border-radius: 13px;
				
			}
		}
		.balance_record{
			margin: 10px 0;
			display: flex;
			justify-content: space-evenly;
			>view{
				padding: 8px 20px;
				border: 1px solid #53DB98;
				border-radius: 10px;
			}
		}
		.recharge_money{
			margin: 20px 12px;
			background-color: #fff;
			border-radius: 8px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.money{
				flex: 1;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-around;
				padding-top: 15px;
				.money_item{
					width: 157px;
					height: 77px;
					background-color: #E6FFFF;
					color: #1AD381;
					border-radius: 10px;
					display: flex;
					flex-direction: column;
					margin-bottom: 10px;
					justify-content: space-around;
					background: url('https://oos.kmey.cn/95/74c4270f930973efb8a24542ca2870.png');
					background-size: 100% 100%;
					border: 2px solid #c7d3c9;
					.principal{
						text-align: center;
						font-size: 30px;
						font-weight: bold;
						margin-top: 10px;
					}
					.give{
						text-align: right;
						margin-right: 10px;
					}
				}
			}
			.technician{
				    margin: 3px 34px;
					display: flex;
					align-items: center;
				.title{
					font-size: 14px;
				}
				>input{
					border-bottom: 1px solid #f2f2f2;
				}
			}
			.custom{
				margin: 13px 34px;
				margin-bottom: 20px;
				background-color: #F2F2F2;
				border-radius: 10px;
				font-size: 14px;
				>input{
					height: 36px;
					text-indent: 10px;
					text-align: center;
				}
			}
		}
		.submit{
			height: 44px;
			line-height: 44px;
			margin: 20px 12px;
			color: #fff;
			border-radius: 30px;
			    background-color: rgb(36, 195, 115);
			font-size: 20px;
			font-weight: bold;
			text-align: center;
		}
		.explain{
			margin: 20px 15px;
			>p{
				font-size:12px;
				color: #ABABAB;
			}
		}
	}
	.active{
		border-color:#3AE890 !important;
	}
	.payy{
		position: fixed;
		top: -20px;
		left: 0;
		padding: 0;
		bottom: 0;
		width: 100%;
		height: 100%;
		right: 0;
		flex-direction: column;
		background: rgba(0,0,0,0.5);
		transition: all 3s linear; 
		.top_seat{
			flex: 1;
			background: rgba(0,0,0,0.1);
			opacity: 0.5;
			z-index: 100;
			width: 100%;
		}
		.content{
			background-color: #f7f7f7;
			padding: 40rpx;
			width: 100%;
			// height: 270px;
			box-sizing: border-box;
			position: absolute;
			bottom: 0;
			left: 0;
			.head{
				text-align: right;
				.cancel{
					color: #9f9f9f;
					font-size: 26rpx;
				}
			}
			.payList{
				padding: 40rpx 0;
				.list{
					display: flex;
					align-items: center;
					padding: 25rpx 30rpx;
					background-color: #fff;
					border-radius: 10rpx;
					margin-bottom: 30rpx;
					border: 1rpx solid #ffc948;
					&:last-child{
						border: 1rpx solid #24c373;
					}
					>image{
						width: 60rpx;
						height: 60rpx;
					}
					>text{
						font-size: 25rpx;
						font-weight: bold;
						margin-left: 12rpx;
					}
					
				}
				
			}
		}
	}
</style>
